﻿using AuthCenter.Domain;
using AuthCenter.ErorCode;
using AuthCenter.Infrastructure;
using AuthCenter_API.Write.Applications.Commands.User;
using AutoMapper;
using MediatR;

namespace AuthCenter_API.Write.Applications.CommandHandlers.User
{
    public class DelUserCommandHandler : IRequestHandler<DelUserCommand, APIResult<object>>
    {
        private readonly IBaseRepository<UserModel> _userRepository;
        private readonly IMapper _mapper;

        public DelUserCommandHandler(IBaseRepository<UserModel> userRepository, IMapper mapper)
        {
            _userRepository = userRepository;
            _mapper = mapper;
        }

        public async Task<APIResult<object>> Handle(DelUserCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            result.Code = APIEnum.DelSuccess;
            result.Message = APIEnum.DelSuccess.Desc();
            UserModel user = _mapper.Map<UserModel>(request);
            user.UpdateTime = DateTime.Now;
            user.IsDel = 1;
            await _userRepository.Update(user);
            return await Task.FromResult(result);
        }
    }
}
